package com.yanwq.sayhi.base;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

import com.yanwq.sayhi.widget.MProgressDialog;

import butterknife.ButterKnife;

/**
 * 作者：yanwq。
 * 日期：2016/1/26 12:25。
 */
public abstract class BaseActivity extends AppCompatActivity {

    private MProgressDialog mPD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPD = new MProgressDialog(this);
        mPD.setCanceledOnTouchOutside(false);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        ButterKnife.bind(this);
        init();
        initView();
    }

    public abstract void init();

    public abstract void initView();

    public void hideStatusBar(boolean enable) {
        WindowManager.LayoutParams p = this.getWindow().getAttributes();
        if (enable) {

            p.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;//|=：或等于，取其一

        } else {
            p.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);//&=：与等于，取其二同时满足，     ~ ： 取反

        }
        getWindow().setAttributes(p);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    public void showProgressDialog(String message) {
        mPD.setMessage(message);
        if (!mPD.isShowing()) {
            mPD.show();
        }
    }

    public void cancelProgressDialog() {
        if (mPD.isShowing()) {
            mPD.cancel();
        }
    }

    public void post(Runnable runnable) {
        getWindow().getDecorView().post(runnable);
    }

    public void post(Runnable runnable, int delayMillis) {
        getWindow().getDecorView().postDelayed(runnable, delayMillis);
    }

    public void delayFinish() {
        delayFinish(1000);
    }

    public void delayFinish(int delayMillis) {
        post(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, delayMillis);
    }
}
